<template>
	<view class="login">
    <image src="../../static/login.png" class="logincover"></image>
    <form>
      <input  placeholder="请输入账号" maxlength="11" class="input" @input="accountInput"></uni-easyinput>
      <input type="password" class="input" maxlength="16" name=""  @input="psdInput" placeholder="请输入密码"></uni-easyinput>
      <button  @click="loginBtn"  class="btn">登录</button>
      <p style="color: red; margin-left: 10vw; margin-top: 2vh;">{{tink}}</p>
    </form>
  </view>
</template>

<script>
export default {
	data() {
		return {
      account:'',
      password:'',
      msg:'',
      tink:'',
      uid:'',
			userInfo: {
				avatarUrl: '',
				city: '',
				country: '',
				province: '',
				gender: 0,
				nickName: '',
			}
		};
	},
	onLoad() {

	},
  created() {
  },
  onReady() {
  },
	methods: {
		handleGetUserInfo() {
			// 获取用户信息
			uni.getUserProfile({
				lang: 'zh_CN',
				desc: '登录',
				success: res => {
					// debugger;
          console.log("account"+this.account)
          console.log(res)
					this.userInfo = res.userInfo; //{ avatarUrl, city, country, province, gender, nickName }
          uni.setStorageSync('username',this.userInfo.nickName)
					uni.setStorageSync('avatar',this.userInfo.avatarUrl)

          console.log(uni.getStorageSync('username'))
          this.flag()
				},
				fail: res => {
					debugger;
					console.log(res);
				}
			});



		},
		flag(){
			// let userinfo = uni.getStorageSync('username');
			// if(userinfo){
			// 	console.log("----")
			// 	uni.switchTab({
			// 		url:"../index/index"
			// 	})
			// }
      let a = uni.getStorageSync('account')
      let p = uni.getStorageSync('password')
      let u = uni.getStorageSync('username')
      if(a){
        if(p){
          if(u){
            uni.switchTab({
              url:"../index/index"
            })
          }
        }
      }
		},
    register(){
      let userinfo = uni.getStorageSync('username');
      uni.request({
      	url:'http://z7jxxt5.nat.ipyingshe.com/insert/user',
      	method:'POST',
      	data:{
          name:userinfo,
          ac:this.account,
          pw:this.password
      	},
      	success:(res)=>{
          console.log("hello")
          console.log(res)
          this.msg=res.data.return_msg
          this.uid = res.data.return_uid
          console.log(this.msg)
          if(this.msg=="注册成功"||this.msg=="登录成功"){
            uni.setStorageSync('account',this.account)
            uni.setStorageSync('password',this.password)
            uni.setStorageSync('uid',this.uid)
           
            this.flag()
          }else{
            this.tink="请检查账号密码是否错误"
          }
      		}
      });
    },
    accountInput(e){
      this.account=e.target.value
      console.log(this.account)
    },
    psdInput(e){
      this.password =e.target.value
      console.log(this.password)
    },
    loginBtn(){
      let userinfo = uni.getStorageSync('username');
      if(userinfo){
        this.register()
      }else{
        this.handleGetUserInfo()
        if(userinfo){
          this.register()
          this.flag()
        }
      }
    }
	}
};
</script>

<style lang="scss">
  body{
  }
	.login{
	}
  .logincover{
    width: 30vw;
    height: 15vh;
    margin-left: 35vw;
    margin-top: 10vh;
    margin-bottom: 3vh;
  }
  .input{
    height: 6vh;
    line-height: 6vh;
    width: 80vw;
    margin-left: 10vw;
    margin-top: 1vh;
    border-radius: 20px;
    padding-left: 10px;
    box-shadow: 0px 0px 3px #999999;
    box-sizing: border-box;
  }
  .btn{
    background-color: #4CD964;
    color: white;
    margin-top: 2vh;
    width: 80vw;
    margin-left: 10vw;
    border-radius: 10px;
  }
  form{
    margin-left: 10vw;
  }
</style>
